<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    timeoutPromise = (delay) => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          //   console.log(1)
          resolve(`延迟了${delay}秒`)
        }, delay * 1000)
      })
    }
    const p1 = timeoutPromise(1)
    const p2 = timeoutPromise(2)
    const p3 = timeoutPromise(3)
    const p4 = timeoutPromise(4)
    const p5 = new Promise((resolve, reject) => {
      setTimeout(() => {
        // console.log(1)
        reject('失败了')
      }, 1000);
    })
    //Promise.all里面传的是一个数组（数组的每一项都是一个promise）,只有每一个promise都成功了，才会走进.then,有一个失败了就会走进.catch
    const ab = Promise.all([p1, p2, p3, p4])
      .then(res => console.log(res))
      .catch(err => console.log(err))
    console.log(ab)
    // Promise.race里面传的也是一个数组（数组里面的每一项都是一个promise）， 等待最先成功的那一个，只要有一个成功就会走进.then
    Promise.race([p1, p2, p3, p4, p5])
      .then(res => { console.log('333', res) })
      .catch(err => { console.log('err', err) })
  </script>
</body>

</html>